package com.xm.dao.background;

import com.xm.entity.Administrator;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * 管理员账号Dao
 *
 * @author 康文程
 * @date 2023-12-15
 */
public class AdministratorDao extends BaseUtil {


    /**
     * 查询账号信息
     *
     * @return 账号列表
     */
    public List<Administrator> findId() {
        // SQL语句
        @Language("SQL")
        String sql = "select administrator_id from administrator";
        // 执行查询操作order by date_entry desc
        ResultSet rs = null;
        List<Administrator> administratorList = new ArrayList<>();
        try {
            rs = executeQuery(sql);
            // 遍历结果集
            while (rs.next()) {
                Administrator administrator = new Administrator();
                administrator.setAdministratorId(rs.getString("administrator_id"));
                administratorList.add(administrator);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(rs);
        }
        return administratorList;
    }

    /**
     * 添加或修改账号
     * 如果传了getAdministratorId则修改没有则新增
     *
     * @param administrator 账号实体类
     * @throws SqlErrorException SQL执行异常
     */
    public boolean addAdministrator(Administrator administrator) throws SqlErrorException {
        if (administrator.getAdministratorId() == null) {
            // 添加账号
            @Language("sql")
            String sql = "INSERT INTO administrator (password, permissions, administrator_state) VALUES (?, ?, ?)";
            int id = executeUpdate(sql, administrator.getPassword(), administrator.getPassword(), administrator.getAdministratorState());
            return id != 0;
        } else {
            // 修改账号
            @Language("sql")
            String sql = "UPDATE administrator SET password = ?, permissions = ?, administrator_state = ? WHERE administrator_id = ?";
            int id = executeUpdate(sql, administrator.getPassword(), administrator.getPermissions(), administrator.getAdministratorState(), administrator.getAdministratorId());
        }
        return false;
    }

    /**
     * 查询后台账号是否存在
     *
     * @param administratorId 账号
     * @param password        密码
     * @return true存在 false不存在
     */
    public boolean checkAdministratorCredentials(String administratorId, String password) {
        @Language("sql")
        String sql = "SELECT COUNT(*) FROM administrator WHERE administrator_id = ? AND password = ? AND administrator_state = 0";
        ResultSet rs = null;
        try {
            rs = executeQuery(sql, administratorId, password);
            if (rs.next()) {
                int count = rs.getInt(1);
                return count > 0;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(rs);
        }
        return false;
    }

    /**
     * 根据权限查询管理员账号
     *
     * @return 符合条件的管理员账号列表
     */
    public List<Administrator> getAdministratorsWithPermissions(String permissionsId) {
        List<Administrator> administrators = new ArrayList<>();
        @Language("sql")
        String sql = "SELECT * FROM administrator WHERE FIND_IN_SET('?', permissions) > 0";
        ResultSet rs = null;
        try {
            rs = executeQuery(sql, permissionsId);
            while (rs.next()) {
                Administrator administrator = new Administrator();
                administrator.setAdministratorId(rs.getString("administrator_id"));
                administrator.setPermissions(rs.getString("permissions"));

                administrators.add(administrator);
            }
        } catch (SQLException e) {
            throw new RuntimeException("SQL报错", e);
        } finally {
            close(rs);
        }
        return administrators;
    }
}
